﻿// $Id: ValidationRule.cs 10 2008-02-29 15:58:02Z mcartoixa $

/* ***************************************************************************
 *                                                                           *
 * Copyright © 2007 NourY Solutions                                          *
 *                                                                           *
 * This file is part of DataRules.                                           *
 *                                                                           *
 * DataRules is free software; you can redistribute it and/or modify it      *
 * under the terms of the GNU Lesser General Public License as published     *
 * by the Free Software Foundation; either version 3 of the License, or      *
 * (at your option) any later version.                                       *
 *                                                                           *
 * DataRules is distributed in the hope that it will be useful, but          *
 * WITHOUT ANY WARRANTY; without even the implied warranty of                *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU          *
 * Lesser General Public License for more details.                           *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public          *
 * License along with this program.  If not, see                             *
 * <http://www.gnu.org/licenses/lgpl.html>.                                  *
 *                                                                           *
 *************************************************************************** */

/*! \file
 * \brief Definition of \c Salamanca.DataRules.EnterpriseLibrary.ValidationRule.
 *
 * \author $Author: mcartoixa $
 * \date $Date: 2008-02-29 16:58:02 +0100 (ven., 29 fÃ©vr. 2008) $
 * \version $Revision: 10 $
 */

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Text;
using NHibernate.Validator;
using NHibernate.Validator.Engine;

namespace Salamanca.DataRules.NHibernate
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary>Business rule based on a <see cref="IClassValidator" /> instance.</summary>
    ///
    ///////////////////////////////////////////////////////////////////////////

    internal class ValidationRule:
        Rule
    {
        public ValidationRule():
            base(string.Empty, string.Empty, RuleValidationMode.Always)
        {
        }

#pragma warning disable 618
        public override bool Validate(object target, string propertyName)
        {
            InvalidValue[] results;

            if (string.IsNullOrEmpty(propertyName))
                results=ValidatorEngine.Validate(target);
            else
                results=ValidatorEngine.ValidatePropertyValue(target, propertyName);

            _Description=GetMessage(results);

            return results.Length==0;
        }
#pragma warning restore 618

        protected override bool DoValidate(object target)
        {
            throw new NotImplementedException();
        }

        private static string GetMessage(IEnumerable<InvalidValue> results)
        {
            StringBuilder ret=new StringBuilder();

            foreach (InvalidValue iv in results)
                ret.AppendLine(iv.Message);

            return ret.ToString();
        }

        /// <summary>The description of the rule.</summary>
        public override string Description
        {
            get
            {
                if (string.IsNullOrEmpty(_Description))
                    return base.Description;

                return _Description;
            }
        }

        private static ValidatorEngine ValidatorEngine
        {
            get
            {
                if (_ValidatorEngine==null)
                    _ValidatorEngine=new ValidatorEngine();
                return _ValidatorEngine;
            }
        }

        private string _Description;

        private static ValidatorEngine _ValidatorEngine;
    }
}
